package com.bluse;

import com.bluse.config.ApplicationProperties;
import com.bluse.config.MyAppilcationListener;
import com.bluse.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Describe: TODO
 * @Author: bluse
 * @Date: 2018/12/25 18:16
 **/

@SpringBootApplication
@EnableConfigurationProperties({ApplicationProperties.class})
public class TestApplication {

    private static final Logger logger = LoggerFactory.getLogger(TestApplication.class);

    public static void main(String[] args) {

        SpringApplication app =  new SpringApplication(TestApplication.class);

        app.addListeners((ApplicationListener<ApplicationEvent>) event -> System.out.println("hello"));

        Environment env =
                app.run(TestApplication.class, args).getEnvironment();
        logApplicationStartup(env);
    }

    @PostConstruct
    private void initApplication() {
        logger.info("Mongo Test  Application will start");
    }


    private static void logApplicationStartup(Environment env) {
        String protocol = "http";
        if (env.getProperty("server.ssl.key-store") != null) {
            protocol = "https";
        }
        String serverPort = env.getProperty("server.port");
        String contextPath = env.getProperty("server.servlet.context-path");
        String swaggerUi = "swagger-ui.html";
        String apidoc = "apidoc/index.html";
        String hostAddress = "localhost";
        try {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            logger.warn("The host name could not be determined, using `localhost` as fallback");
        }
        logger.info("\n----------------------------------------------------------\n\t" +
                        "Application '{}' is running! Access URLs:\n\t" +
                        "Local: \t\t{}://localhost:{}{}\n\t" +
                        "External: \t{}://{}:{}{}\n\t" +
                        "Swagger-ui: {}://localhost:{}/{}\n\t" +
                        "apiDocs: \t{}://localhost:{}/{}\n\t" +
                        "\n----------------------------------------------------------",
                env.getProperty("spring.application.name"),
                protocol, serverPort, contextPath,
                protocol, hostAddress, serverPort, contextPath,
                protocol, serverPort, swaggerUi,
                protocol, serverPort, apidoc
        );
    }
}

